﻿#include "htmlcfgclass.h"
#include <qsciscintilla.h>
#include <QMessageBox>
#include <QRegExpValidator>

HtmlCfgclass::HtmlCfgclass(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	connect(ui.SaveButton, SIGNAL(clicked()), this, SLOT(on_savefile()));
	ui.comboBox_1->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));
	ui.comboBox_2->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));
	ui.comboBox_3->setValidator(new QRegExpValidator(QRegExp("[0-9]+$")));
	ui.plainTextEdit_1->setPlaceholderText("cfif, cfelse, math, mroot, mrow, mi, mn, mo, msqrt, mfrac, msubsup, munderover,"
			" munder, mover, mmultiscripts, msup, msub, mtext,"
			" mprescripts, mtable, mtr, mtd, mth");
	ui.plainTextEdit_2->setPlaceholderText("cfoutput, cfquery");
	ui.plainTextEdit_3->setPlaceholderText("cfelse");
}

HtmlCfgclass::~HtmlCfgclass()
{}

void createDefineConfig(const char * data)
{
	const char* configPath = "tidydefine.cfg";
	FILE* defaultConfig = fopen(configPath, "wb");
	if (NULL != defaultConfig)
	{
		fprintf(defaultConfig, data);
		fclose(defaultConfig);
	}
	QMessageBox::information(nullptr, "result", "save success", QMessageBox::Yes, QMessageBox::Yes);
}
static QString checkboxConfig(Ui::htmlcfgClassClass ui,QString saveText)
{
	if (!ui.checkBox_1->isChecked())
	{
		saveText += "drop-empty-paras: no\r\n";
	}

	if (ui.checkBox_2->isChecked())
	{
		saveText += "break-before-br: yes\r\n";
	}

	if (ui.checkBox_3->isChecked())
	{
		saveText += "sort-attributes: alpha\r\n";
	}

	if (ui.checkBox_4->isChecked())
	{
		saveText += "indent-attributes: yes\r\n";
	}

	if (ui.checkBox_5->isChecked())
	{
		saveText += "vertical-space: yes\r\n";
	}

	if (!ui.checkBox_6->isChecked())
	{
		saveText += "wrap-asp: no\r\n";
	}

	if (ui.checkBox_7->isChecked())
	{
		saveText += "input-xml: no\r\n";
	}
	else
	{
		saveText += "input-xml: yes\r\n";
	}

	if (!ui.checkBox_8->isChecked())
	{
		saveText += "wrap-jste: no\r\n";
	}

	if (!ui.checkBox_9->isChecked())
	{
		saveText += "wrap-php: no\r\n";
	}

	if (ui.checkBox_10->isChecked())
	{
		saveText += "markup: yes\r\n";
	}

	if (ui.checkBox_11->isChecked())
	{
		saveText += "clean: yes\r\n";
	}

	if (ui.checkBox_12->isChecked())
	{
		saveText += "quote-nbsp: yes\r\n";
	}

	if (!ui.checkBox_13->isChecked())
	{
		saveText += "wrap-sections: no\r\n";
	}

	if (ui.checkBox_14->isChecked())
	{
		saveText += "wrap-script-literals: yes\r\n";
	}
	if (ui.checkBox_15->isChecked())
	{
		saveText += "punctuation-wrap: yes\r\n";
	}
	if (ui.checkBox_16->isChecked())
	{
		saveText += "drop-font-tags: yes\r\n";
	}
	if (ui.checkBox_17->isChecked())
	{
		saveText += "decorate-inferred-ul: yes\r\n";
	}
	if (ui.checkBox_18->isChecked())
	{
		saveText += "enclose-text: yes\r\n";
	}
	if (!ui.checkBox_19->isChecked())
	{
		saveText += "fix-backslash: no\r\n";
	}
	if (ui.checkBox->isChecked())
	{
		saveText += "hide-comments: yes\r\n";
	}
	return saveText;
}

QString outputTextEditConfig(Ui::htmlcfgClassClass ui, QString saveText)
{
	if (ui.outcheckBox_1->isChecked())
	{
		saveText += "output-xml: yes\r\n";
	}
	if (ui.outcheckBox_2->isChecked())
	{
		saveText += "output-xhtml: yes\r\n";
	}
	if (ui.outcheckBox_3->isChecked())
	{
		saveText += "output-html: yes\r\n";
	}
	return saveText;
}

QString comboBoboxConfig(Ui::htmlcfgClassClass ui, QString saveText)
{
	if (ui.comboBox_1->currentText().toInt() != 68)
		saveText += "wrap: " + ui.comboBox_1->currentText() + "\r\n";
	if (ui.comboBox_2->currentText().toInt() != 2)
		saveText += "indent-spaces: " + ui.comboBox_2->currentText() + "\r\n";
	if (ui.comboBox_3->currentText().toInt() != 8)
		saveText += "tab-size: " + ui.comboBox_3->currentText() + "\r\n";
	if (ui.comboBox_4->currentText() != "auto")
		saveText += "indent: " + ui.comboBox_4->currentText() + "\r\n";
	return saveText;
}

QString plainTextEditConfig(Ui::htmlcfgClassClass ui, QString saveText)
{
	if(!ui.plainTextEdit_1->toPlainText().isEmpty())
		saveText += "new-inline-tags: " + ui.plainTextEdit_1->toPlainText() +"\r\n";
	if (!ui.plainTextEdit_2->toPlainText().isEmpty())
		saveText += "new-blocklevel-tags: " + ui.plainTextEdit_2->toPlainText() + "\r\n";
	if (!ui.plainTextEdit_3->toPlainText().isEmpty())
		saveText += "new-empty-tags: " + ui.plainTextEdit_3->toPlainText() + "\r\n";
	return saveText;
}

void HtmlCfgclass::on_savefile()
{
	QString saveText = "";
	saveText = outputTextEditConfig(ui, saveText);
	saveText = checkboxConfig(ui,saveText);
	saveText = comboBoboxConfig(ui, saveText);
	saveText = plainTextEditConfig(ui, saveText);
	createDefineConfig(saveText.toStdString().c_str());
}

